package com.sbr.tbd.wicket.panels.feature;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.tbd.model.Application;
import com.sbr.tbd.model.Feature;
import com.sbr.tbd.services.AdministrationService;
import com.sbr.tbd.wicket.pages.administration.feature.ListFeaturesPage;

public class EditFeaturePanel extends Panel {

	protected static final Logger LOG = LoggerFactory.getLogger(EditFeaturePanel.class);
	
	@SpringBean
	private AdministrationService administrationService;
	
	private class EditFeatureForm extends Form<Feature> {

		public EditFeatureForm(String id) {
			super(id);
			setDefaultModel(new CompoundPropertyModel<Application>(new Feature()));
			
			add(new RequiredTextField<String>("name"));
			
		}
		
		@Override
		protected void onSubmit() {
			LOG.debug("onSubmit called");
			Feature feature = getModelObject();
			administrationService.addFeature(feature);
			setResponsePage(ListFeaturesPage.class);
		}
		
	}
	
	public EditFeaturePanel(String id) {
		super(id);

		add(new EditFeatureForm("addFeatureForm"));
		add(new FeedbackPanel("feedbackPanel"));
	}

	public void setAdministrationService(AdministrationService administrationService) {
		this.administrationService = administrationService;
	}
	
}
